package com.iespring.service.app;

import com.iespring.bean.admin.goods.po.MallCategory;
import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.bean.app.user.entity.UserInfo;
import com.iespring.bean.app.user.vo.*;
import com.iespring.mapper.admin.*;
import com.iespring.util.UserInfoUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xD
 * @since 2022/12/31 20:30
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    @Resource
    private MallGoodsMapper mallGoodsMapper;

    @Resource
    private MallOrderMapper mallOrderMapper;

    @Resource
    private MallAdMapper mallAdMapper;

    @Resource
    private MallBrandMapper mallBrandMapper;

    @Resource
    private MallCategoryMapper mallCategoryMapper;

    @Resource
    private MallCouponMapper mallCouponMapper;

    @Resource
    private MallTopicMapper mallTopicMapper;

    @Override
    public WxLoginVo login(UsernamePasswordToken token) {
        //获取Subject对象
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        //判断是否通过验证
        if (subject.isAuthenticated()) {
            //获取认证成功后的用户信息
            MallUser mallUser = (MallUser) subject.getPrincipals().getPrimaryPrincipal();
            //SessionId
            Serializable sessionId = subject.getSession().getId();
            UserInfo userInfo = new UserInfo();
            userInfo.setAvatarUrl(mallUser.getAvatar());
            userInfo.setNickName(mallUser.getNickname());
            WxLoginVo wxLoginVo = new WxLoginVo();
            wxLoginVo.setUserInfo(userInfo);
            wxLoginVo.setToken((String) sessionId);
            return wxLoginVo;
        }
        return null;
    }

    @Override
    public WxUserOrderVo userIndex() {
        //获取属性名
        Field[] fields = WxUserIndexVo.class.getDeclaredFields();
        Integer status;
        WxUserIndexVo wxUserIndexVo = new WxUserIndexVo();
        Integer userId = UserInfoUtils.getUser().getId();
        for (Field field : fields) {
            if ("unship".equals(field.getName())) {
                status = 201;
                wxUserIndexVo.setUnship(mallOrderMapper.selectOrderStatusByUserId(userId, field.getName(), status));
            }
            if ("unrecv".equals(field.getName())) {
                status = 301;
                wxUserIndexVo.setUnrecv(mallOrderMapper.selectOrderStatusByUserId(userId, field.getName(), status));
            }
            if ("unpaid".equals(field.getName())) {
                status = 101;
                wxUserIndexVo.setUnpaid(mallOrderMapper.selectOrderStatusByUserId(userId, field.getName(), status));
            }
        }
        //通过IN查询 获取comment为0的订单数
        wxUserIndexVo.setUncomment(mallOrderMapper.selectOrderGoodsByUserId(userId));

        return new WxUserOrderVo(wxUserIndexVo);
    }

    @Override
    public WxUserHomeIndexVo homeIndex() {
        List<MallCategory> goodsCategoryInfo = mallCategoryMapper.getGoodsCategoryInfoByPid(0);
        WxUserHomeIndexVo wxUserHomeIndexVo = new WxUserHomeIndexVo();
        //position 广告位置：1则是首页
        wxUserHomeIndexVo.setBanner(mallAdMapper.selectByPosition(1));
        //显示前六条
        wxUserHomeIndexVo.setBrandList(mallBrandMapper.selectAll());
        wxUserHomeIndexVo.setChannel(goodsCategoryInfo);
        wxUserHomeIndexVo.setCouponList(mallCouponMapper.selectAll());
        wxUserHomeIndexVo.setHotGoodsList(mallGoodsMapper.selectIsHot());
        wxUserHomeIndexVo.setNewGoodsList(mallGoodsMapper.selectIsNew());
        wxUserHomeIndexVo.setTopicList(mallTopicMapper.selectAllTopic());
        //获取类目id&name
        List<WxGoodsListVo> floorGoodsList = new ArrayList<>();
        List<MallCategory> goodsCategoryInfo1 = mallCategoryMapper.getGoodsCategoryInfo();
        for (MallCategory mallCategory : goodsCategoryInfo1) {
            //需要分页,只显示前四条内容
            List<WxMallGoodsVo> mallGoods = mallGoodsMapper.selectGoodsByCategoryId(mallCategory.getId());
            if (!ObjectUtils.isEmpty(mallGoods)) {
                WxGoodsListVo wxGoodsListVo = new WxGoodsListVo();
                wxGoodsListVo.setGoodsList(mallGoods);
                wxGoodsListVo.setId(mallCategory.getId());
                wxGoodsListVo.setName(mallCategory.getName());
                //控制显示条数
                if (floorGoodsList.size() <= 6) {
                    floorGoodsList.add(wxGoodsListVo);
                }
            }
        }
        wxUserHomeIndexVo.setFloorGoodsList(floorGoodsList);
        return wxUserHomeIndexVo;
    }
}
